<template>
  <base-breadcrumb :title="$route.meta.title" :breadcrumb="!isAudit">
    <!-- 职工合理化建议编辑 -->
    <a-form-model
      ref="ruleForm"
      :model="form"
      :label-col="{ span: 6 }"
      :wrapper-col="{ span: 18 }"
      :rules="editModel != 0 ? rules : {}"
    >
      <a-spin :spinning="loading[0] || loading[1]">
        <!-- 表格 -->
        <a-card :bordered="false">
          <title-name title="基本信息"></title-name>
          <custom-form :formList="formList" :form="form" :editModel="editModel == 0 ? 'V' : ''" @getOption="getOption">
            <!-- 附件上传 -->
            <template #fileInfoVOS>
              <template v-if="editModel != 0">
                <l-table businessCode="UNION2" :uploadArrys.sync="form.fileInfoVOS"></l-table>
                <div class="file_prompt">支持扩展名：.rar .zip .doc .docx .pdf .jpg...</div>
              </template>
              <file-link v-else :fileList="form.fileInfoVOS" style="margin-top: -3px"></file-link>
            </template>
          </custom-form>
        </a-card>
        <a-card v-if="form.replyOpinion" :bordered="false">
          <title-name title="回复"></title-name>
          <a-form-model-item prop="replyOpinion" label="意见" class="requiredFormItem">
            <div style="margin-top: -3px">
              {{ form.replyOpinion || '无' }}
            </div>
          </a-form-model-item>
          <a-form-model-item prop="replyFileInfoVOS" label="附件" class="requiredFormItem">
            <file-link :fileList="form.replyFileInfoVOS" style="margin-top: -3px"></file-link>
          </a-form-model-item>
        </a-card>
      </a-spin>
      <!-- 页脚操作栏 -->
      <div v-if="!isConsultation" class="btn_box page-btn-right-top" >
        <a-button
          :loading="loading[0]"
          :disabled="loading[1]"
          v-if="(editModel == 1 && (statusCode == '0' || statusCode == '3')) || editModel == 2"
          type="primary"
          class="button_right"
          @click="handleSubmit(0)"
          >{{ statusCode == '2' || statusCode == '3' ? '保存' : '保存草稿' }}</a-button
        >
        <a-button
          :loading="loading[1]"
          :disabled="loading[0]"
          v-if="
            (editModel == 1 && (statusCode == '0' || statusCode == '3')) ||
            editModel == 2 ||
            (editModel != 0 && statusCode == '2')
          "
          type="primary"
          class="button_right"
          @click="handleSubmit(1)"
          >提交</a-button
        >
        <a-button type="primary" @click="handleActiveClose">关闭</a-button>
      </div>
    </a-form-model>
      <selectApprover :businessId="businessId"  :processConfig="setProcessConfig"></selectApprover>
  </base-breadcrumb>
</template>

<script>
import { workersRationalizationSuggestions } from '@/api/union/workersCongress' // api
import BaseEditPage from '@/views/baseEditPage' // 编辑详情页基础配置
import Selector from '@/components/Select/Selector'
import SelectTreeUnion from '@/components/Select/SelectTreeUnion' // 工会

export default {
    name:'a'+Date.now(),
  extends: new BaseEditPage(),
  props: {
    // 是否作为审批详情
    isAudit: {
      type: Boolean,
      default: false
    },
    // 是否作为意见征询
    isConsultation: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      loading: { 0: false, 1: false }, // 草稿和提交按钮loding
      editModel: 0, // 操作状态:0 查看;1 编辑;2 新增
      form: {
        creator: '',
        createTime: '',
      }, // 表单
      rules: {
        title: [{ required: true, whitespace: true, message: '请输入荣誉名称', trigger: 'change' }],
        typeId: [{ required: true, message: '请选择建议级别', trigger: 'change' }],
        unionId: [{ required: true, message: '请选择工会组织', trigger: 'change' }],
        content: [{ required: true, whitespace: true, message: '请输入主要内容', trigger: 'change' }],
        fileInfoVOS: [{ type: 'array', required: true, message: '请上传佐证材料', trigger: 'blur' }],
      }, // 校验规则
      init: false, // 初始化控件
      //获取流程配置值
      processConfig: null,
      //点击提交发送流程配置值
      setProcessConfig: null,
      businessId: null,
      statusCode:null,
}
  },
  watch: {
    // 监听附件实现附件校验
    'form.fileInfoVOS'() {
      if (this.editModel != 0 && this.init) {
        this.$nextTick(() => {
          this.$refs.ruleForm?.validateField('fileInfoVOS')
        })
      }
    },
  },
 async mounted() {
    this.$route.meta.title = ['职工合理化建议查看', '职工合理化建议编辑', '职工合理化建议新增'][this.editModel]
    // 不为添加时请求详情
    if (this.editModel != 2) {
      this.loading[0] = true
      workersRationalizationSuggestions
        .getDetail({id:this.id})
        .then((res) => {
          this.loading[0] = false
          this.form = res.data
          this.statusCode = res.data?.status
          this.$nextTick(() => {
            this.init = true
          })
        })
        .catch(() => {
          this.loading[0] = false
        })
    } else {
      this.$nextTick(() => {
        this.form.creator = this.$userInfo().name
        this.form.createTime = this.$dateFormat(new Date(), 'D')
        this.init = true
      })
    }
    //获取通过获取流程配置值
    this.processConfig = await this.$getConfigFn('union_workers_suggestions') 
  },
  computed: {
    formList() {
      return [
        {
          keyName: 'title',
          type: 'BaseInput',
          label: '主题',
        },
        {
          keyName: 'typeId',
          viewName: 'typeName',
          label: '建议级别',
          disabled:true,
          placeholder: '请先选中工会组织',
          type: Selector,
          attrs: {
            selectType: '1',
            urlType: 'union',
          },
        },
        {
          keyName: 'unionId',
          viewName: 'union',
          label: '工会组织',
          type: SelectTreeUnion,
        },
        {
          keyName: 'creator',
          type: 'a-input',
          disabled: true,
          label: '填报人',
        },
        {
          keyName: 'createTime',
          type: 'a-input',
          disabled: true,
          label: '填报时间',
        },
        {
          keyName: 'content',
          type: 'BaseTextarea',
          label: '主要内容',
          span: 24,
        },
        {
          slot: true,
          keyName: 'fileInfoVOS',
          label: '佐证材料',
          span: 24,
        },
      ]
    },
  },
  methods: {
    /**
     * 回调工会组织选中项
     */
    getOption(){
      if(arguments[0] === 'unionId'){
        let levelId = arguments[1].unionLevelId // 层级id
        // 映射层级
        if(this.form.typeId){
          this.form.typeId = levelId
        }else{
          this.$set(this.form,'typeId',levelId)
        }
        /**
         * 添加表单组织机构参数
         */
        // 公司级
        if(levelId === '605004340528287745'){
          this.form.companyId = arguments[1].id
        // 分公司级
        }else if(levelId === '605004401341501446'){
          this.form.branchComId = arguments[1].id
          this.form.companyId = arguments[1].parentId
        // 项目级
        }else if(levelId === '605004528177254447'){
          this.form.projectId = arguments[1].id
          this.form.branchComId = arguments[1].parentId
        }
      }
    },
    /**
     * 提交表单
     */
    handleSubmit(statusCode) {
      let that = this
      that.setProcessConfig = null; 
      let api = ['update', 'create'][that.editModel - 1] // 根据操作切换url地址
      // 校验表单
      that.$refs.ruleForm.validate((e) => {
        // if (e) {
        if (e || statusCode === 0) {
          // 提交表单
          statusCode === 0 && that.$refs.ruleForm.clearValidate()
          let form = that.$clone(that.form)
          // 格式化文件列表
          if (form.fileInfoVOS?.length > 0) {
            form.fileId = form.fileInfoVOS.map((item) => item.commonId).join()
          }
          // 删除多余参数
          delete form.creator
          delete form.createTime
          form.status =
            (that.statusCode == '3' && statusCode == 0) || (that.statusCode == '2' && statusCode == 1)
              ? that.statusCode
              : statusCode

            this.submitApprovalConfirm(statusCode == '1'?this.processConfig:6,(flag)=>{
                if(!flag) {
                    that.loading[auditStatus] = false
                    return
                  }
            that.loading[statusCode] = true
          workersRationalizationSuggestions[api](form)
            .then((res) => {
              that.loading[statusCode] = false
              that.$message.success(['保存', '提交'][statusCode] + '成功！')
              if(statusCode*1 === 1){
                  that.businessId = res.data.id;
                  that.setProcessConfig = this.processConfig;
              }else{
                  that.$multiTab.close()
              }
            })
            .catch(() => {
              that.loading[statusCode] = false
            })
          })    
       
        } else {
          this.$message.warning("请填写完所有必填项信息")
        }
      })
    },
  },
}
</script>

<style lang="less" scoped>
.file_prompt {
  color: #999;
}
</style>
